﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ares.Controller;
using Ares.Engine;
using System.Windows.Input;
using Ares.GUI.Views;
using Ares.GUI.Models;
using System.IO;
using System.Collections.ObjectModel;

namespace Ares.GUI.Controller
{
    public class ProjectController : BaseController
    {
        #region Private Member
        private ObservableCollection<BackupContainer> backupProjects;
        private ICommand newProjectCommand;
        #endregion
 
        #region Public Member
        public ObservableCollection<BackupContainer> BackupProjects
        {
            get
            {
                return backupProjects;
            }
            set
            {
                backupProjects = value;
                OnPropertyChanged("BackupProjects");
            }
        }
        public ICommand NewProjectCommand
        {
            get
            {
                if (this.newProjectCommand == null)
                    this.newProjectCommand = new RelayCommand(p => this.CreateNewProject(p), () => this.CanCreateNewProject());

                return this.newProjectCommand;
            }
        }
        #endregion

        public ProjectController()
        {
            BackupProjects = new ObservableCollection<BackupContainer>();
            LoadProjects(Settings.ProjectsPath);

            Mediator.Register(this, new[]{
                Messages.SaveProject,
                Messages.CancelNewProject
            });
        }

        private void CreateNewProject(object p)
        {
            Mediator.NotifyColleagues(Messages.DoNewProject, null);
        }

        private bool CanCreateNewProject()
        {
            return true;
        }

        public override void MessageNotification(string message, object args)
        {
            switch (message)
            {
                case Messages.SaveProject:
                    if(args!=null)
                    AddToContainer((BackupProject)args);
                    OnPropertyChanged("BackupProjects");
                    break;

                case Messages.CancelNewProject:
                    OnPropertyChanged("BackupProjects");
                    break;
            }
        }

        public void LoadProjects(String Path)
        {
            string[] files = Directory.GetFiles(Path,
                                    "*" + Settings.ProjectFileExtension,
                                    SearchOption.AllDirectories);

            foreach (string file in files)
            {
                try
                {
                    AddToContainer(BackupProject.LoadBackupProject(file));
                }
                catch { }
            }
        }

        private void AddToContainer(BackupProject backupProject)
        {
            if (!BackupProjects.Contains(new BackupContainer
                (
                    backupProject,
                    Path.Combine(Settings.ProjectsPath, backupProject.ProjectGuid + Settings.ProjectFileExtension)
                ), new BackupContainerComparer()))
            {
                BackupProjects.Add(new BackupContainer
                (
                    backupProject,
                    Path.Combine(Settings.ProjectsPath, backupProject.ProjectGuid + Settings.ProjectFileExtension)
                ));
                OnPropertyChanged("BackupProjects");
            }
        }
    }
}